package cn.life.business.audit.service.impl;

import cn.jpush.api.push.model.Message;
import cn.life.business.apply.dao.ApplyDao;
import cn.life.business.apply.entity.vo.ApplyVo;
import cn.life.business.audit.dao.AuditDao;
import cn.life.business.audit.entity.po.AuditPo;
import cn.life.business.audit.service.AuditService;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.po.MessagePo;
import cn.life.common.utils.PDF.apply.AuditTemplate;
import cn.life.common.utils.message.ALiBaBaUtils;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author 王振宇
 * @Description:
 * @Date 2018-09-20 17:13:26
 */
@Service
@Transactional
public class AuditServiceImpl implements AuditService {

    @Autowired
    private ApplyDao applyDao;

    @Autowired
    private AuditDao auditDao;

    @Override
    public CMessage addAudit(AuditPo auditPo) {
        AuditPo auditRec = auditDao.queryAuditInfo(auditPo);
        if(auditRec == null){
            return auditDao.addAudit(auditPo)==1 ? CMessage.addSuccess():CMessage.addSuccess();
        }else{
            return auditDao.updateState(auditPo)==1 ? CMessage.addSuccess():CMessage.addSuccess();
        }
    }

    @Override
    public CMessage updateState(AuditPo auditPo) {
        if(auditDao.updateState(auditPo) == 1){
            //1.获取用户手机号码
            ApplyVo applyVo = new ApplyVo();
            applyVo.setApplyId(auditPo.getApplyId());
            applyVo = applyDao.queryApplyInfo(applyVo);
            //2.发送审核通知
            MessagePo messagePo = new MessagePo();
            messagePo.setPhone(applyVo.getApplyPhone());
            messagePo.setTime(new Date());
            messagePo.setName("(编号：" + applyVo.getApplyNo()+")");
            messagePo.setAuditRes(auditPo.getAuditState());

            try {
                //发送给申请人员
                new ALiBaBaUtils().sendAuditMessage(messagePo);
                //发送给审核人员
                /*String auditPers = applyDao.getAuditPerPhone();
                MessagePo auditMessage = new MessagePo();
                auditMessage.setPhone(auditPers);
                auditMessage.setAuditRes(applyVo.getAuditState());
                new ALiBaBaUtils().sendAuditMessage(auditMessage);*/
            } catch (ClientException e) {
                e.printStackTrace();
                return CMessage.fail("发送短信时遇到未知错误请联系管理人员");
            }
            return CMessage.successByMsg("审核成功");
        }
        return CMessage.fail("审核失败");
    }

    @Override
    public CMessage exportPdf(HttpServletRequest request, AuditPo auditPo) {
        String imgUrl = auditPo.getCertifiedImgUrl();
        auditPo = auditDao.queryAuditInfo(auditPo);
        auditPo.setCertifiedImgUrl(imgUrl);
        String pdfPrefixUrl = request.getServletContext().getRealPath("/") + "/pdf/";
        String imgPrefixUrl = request.getServletContext().getRealPath("/") + "/img/";
        String pdfName ="审查单-" + auditPo.getApplyNo() + ".pdf";
        //1.导出申请单中盖章图片
        boolean res = new AuditTemplate().AuditTemplate(pdfPrefixUrl,pdfName,auditPo,imgPrefixUrl);
        if(res){
            return CMessage.successByMsg("审查单-" + auditPo.getApplyNo() + ".pdf");
        }else{
            return CMessage.fail("导出失败");
        }
    }
}
